package com.jackiechan.day15;

public class Tickets {
    public static void main(String[] args) {
        TThread t = new TThread();
        Thread t1 = new Thread(t, "赵");
        Thread t2 = new Thread(t, "钱");
        Thread t3 = new Thread(t, "孙");
        Thread t4 = new Thread(t, "李");
        t1.start();
        t2.start();
//        t3.start();
//        t4.start();
    }

    static class TThread extends Thread {
        int count = 10003;
        double start = 0;
        double end = 0;
        @Override
        public void run() {
            start = System.nanoTime();
            while (true) {
                synchronized (this) {
                    if (count > 0) {
                        System.out.println(Thread.currentThread().getName() +
                                " 为您服务: 卖出" + (10003 - count + 1) + "号票,余票: " + (--count));
                    } else {
                        System.out.println(Thread.currentThread().getName() +
                                " 为您服务: 已售罄");
                        break;
                    }
                }
            }
            end = System.nanoTime();
            System.out.println(Thread.currentThread().getName() +" "+(end-start)/1000000+"微秒");
        }
    }
}
